-- 本地用户


local fs  = require "nixio.fs"
local sys = require "luci.sys"
local utl = require "luci.util"
local ds =  require "luci.dispatcher"
local http = require "luci.http"
local nt = require "luci.model.nac_nsutil"
local uci = require "luci.model.uci".cursor()


m = SimpleForm("user", translate("User configuration"))

m.redirect = ds.build_url("admin/ac_nac/localuser")
m.reset = false



------定义变量
local user_cfg = {}
local msg 

arg[1] = arg[1] or ""
-- 获取请求的参数
user_name = http.formvalue("user_name") or ""

if arg[1] == "new" then
    act = "new"
elseif arg[1] == "detail" then
     act = "detail"
elseif arg[1] == "edit" then
    act = "edit"
 else
    act = ""
end

if act == "edit" and user_name == "" then
    act = ""
end



-- 当动作为编辑时，查询该用户信息
if act == "edit" or act == "detail" then
	ubusrt = utl.ubus("nac_proxy", "websrv", {act='getuserinfo',data={ ver='1', user_name=user_name}}) or {msg="ubus error",rt_code="0"}
	local rt_code = ubusrt["rt_code"] or ""
	-- http.write("rt_code=".. rt_code .. "\n")
	if rt_code ~= 200 then
		nt.error_render(m, rt_code, ubusrt[msg])
		return m
	end

	msg = ubusrt["msg"] or ""
	user_cfg = {}

	if type(msg) ~= "table" then
	    --不是table，表示服务器存在问题
	    nt.error_render(m, rt_code, "Server Error")
	    return m
	elseif msg.code == 1 then
	    -- 服务返回的code为1，表示该次请求存在错误
	    nt.error_render(m, rt_code, "Request error")
	    return m
	elseif msg.code == 2 then
	    -- 服务返回的code为2，表示该次查询结果为空
	else
	    -- 正确，返回code=200,data为服务返回的内容
	end

	user_cfg = msg["info"]
end

-- -- 展示用户信息
if act == "detail" then
	m.submit = false
	op = m:field(DummyValue, "id", translate("ID"))
	function op.cfgvalue()
    		return user_cfg.id
	end
	op = m:field(DummyValue, "user_name", translate("User name"))
	function op.cfgvalue()
    		return user_cfg.user_name
	end
	op = m:field(DummyValue, "user_password", translate("Password"))
	function op.cfgvalue()
    		return user_cfg.user_password
	end
	op = m:field(DummyValue, "state", translate("state"))
	function op.cfgvalue()
		local info = ""
    		if user_cfg.state == 1 then
    			info = "Enabled"
    		elseif user_cfg.state == 0 then
    			info = "disabled"
    		end
    		return translate(info)
	end
	op = m:field(DummyValue, "rate_down", translate("Download speed"))
	function op.cfgvalue()
    		 return nt.format_rate(user_cfg.rate_down)
	end
	op = m:field(DummyValue, "rate_up", translate("Upload rate"))
	function op.cfgvalue()
    		return nt.format_rate(user_cfg.rate_up)
	end
	op = m:field(DummyValue, "session_time", translate("Length of package"))
	function op.cfgvalue()
    		return nt.format_life(user_cfg.session_time)
	end
	op = m:field(DummyValue, "surplus_time", translate("Surplus Time"))
	function op.cfgvalue()
		if user_cfg.surplus_time ~= nil then
    		return nt.format_life(user_cfg.surplus_time)
    		end
	end
	op = m:field(DummyValue, "used_time", translate("Available Time"))
	function op.cfgvalue()
    		return nt.format_life(user_cfg.used_time)
	end
	op = m:field(DummyValue, "flow", translate("Traffic quota"))
	function op.cfgvalue()
    		return nt.format_traffic(user_cfg.flow)
	end
	op = m:field(DummyValue, "surplus_flow", translate("Surplus quota"))
	function op.cfgvalue()
		if user_cfg.surplus_flow ~= nil then
    			return nt.format_traffic(user_cfg.surplus_flow)
    		end
	end
	op = m:field(DummyValue, "used_flow", translate("used quota"))
	function op.cfgvalue()
    		return nt.format_traffic(user_cfg.used_flow)
	end
	op = m:field(DummyValue, "smart_mac", translate("Bound MAC"))
	function op.cfgvalue()
		local info = ""
    		if user_cfg.smart_mac == 1 then
    			info = "Bind"
    		elseif user_cfg.smart_mac == 0 then
    			info = "Not bind"
    		end
    		return info
	end
	return m

end


-- @option  msg.info.user_name
op = m:field(Value, "user_name", translate("User name"),translate("Legal character: A-Z, A-Z, 0-9 and _, the length range of 1-128"))
op.rmempty = false
op.datatype = "uciname"
op.datatype = "and(minlength(1),maxlength(128))"
function op.cfgvalue()
    return user_cfg.user_name
end



-- @option  msg.info.user_password
pwd = m:field(Value, "user_password", translate("Password"),translate("Legal character: A-Z, A-Z, 0-9 and _, the length range of 1-128"))
pwd.datatype = "uciname"
pwd.datatype = "and(minlength(1),maxlength(128))"
function pwd.cfgvalue()
    return user_cfg.user_password
end


-- @option  msg.info.rate_down
up = m:field(Value, "rate_down", translate("Download speed"),translate("Download speed limit, 0 of the speed limit, the unit is' MB / s', the range of 0 - 4095, the effective value of the decimal point after the two"))
up.rmempty = false
up.datatype = "range(0,4095)"
function up.cfgvalue(self, section)
	local str = ""
	if user_cfg.rate_down ~= nil then
	local down = (user_cfg.rate_down/math.pow(1024, 2))
		str = str .. string.format("%.2f", down)
	end
	return str
end
function up.write(self, section, value)
	user_cfg.rate_down = ""
	user_cfg.rate_down = user_cfg.rate_down .. value * math.pow(1024, 2)
	return user_cfg.rate_down
end


-- @option msg.info.rate_up
down = m:field(Value, "rate_up", translate("Upload rate"),translate("Upload speed limit, 0 of the speed limit, the unit is' MB / s', the range of 0 - 4095, the effective value of the decimal point after the two"))
down.rmempty = false
down.datatype = "range(0,4095)"
function down.cfgvalue(self, section)
	local str = ""
	if user_cfg.rate_up ~= nil then
	local up = (user_cfg.rate_up/math.pow(1024, 2))
		str = str .. string.format("%.2f", up)
	end
	return str
end
function down.write(self, section, value)
	user_cfg.rate_up = "" .. value * math.pow(1024, 2)
	return user_cfg.rate_up
end
-- @option msg.info.session_time
time = m:field(Value, "session_time", translate("Length of package"), translate("The terminal can be used for a long time, 0 that does not limit, the unit is' hour ', range 0 - 1193046, the effective value is after the decimal point two"))
time.rmempty = false
time.datatype  = "range(0,1193046)"
function time.cfgvalue(self, section)
	local str = ""
	if user_cfg.session_time ~= nil then
	local time = (user_cfg.session_time/3600)
		str = str .. string.format("%.2f", time)
	end
	return str
end
function time.write(self, section, value)
	user_cfg.session_time = "" .. value * 3600
	return user_cfg.session_time
end

-- @option msg.info.flow
flow = m:field(Value, "flow", translate("Traffic quota") ,translate("Available flow of the terminal, 0 said no limit, the unit is' Mega ', the range of 0 - 1759218604441, the effective value of the decimal point after the two"))
flow.rmempty = false
flow.datatype = "range(0,1759218604441)"
function flow.cfgvalue(self, section)
	local str = ""
	if user_cfg.flow ~= nil then
	local flow = (user_cfg.flow/math.pow(1024, 2))
		str = str .. flow 
	end
	return str
end
function flow.write(self, section, value)
	user_cfg.flow =  value  
	return user_cfg.flow
end

-- @option msg.info.smart_mac
smart_mac = m:field(ListValue, "smart_mac", translate("Bound MAC") )
smart_mac:value(1, translate("Bind"))
smart_mac:value(0, translate("Not bind"))
function smart_mac.cfgvalue()
    return user_cfg.smart_mac
end



function m.handle(self, state, data)
    if state == FORM_VALID then

    	
	local tb = {}
	local tb_user_name = ""

	if act_edit then
		tb_user_name = user_name
	else
		tb_user_name = data.user_name or user_cfg.user_name or ""
	end

	tb["ver"] = "1"
	tb["user_name"] = tb_user_name
	tb["user_passwd"] = data.user_password or user_cfg.user_password or ""
	tb["rate_up"] = data.rate_up or user_cfg.rate_up or ""
	tb["rate_down"] = data.rate_down or user_cfg.rate_down or ""
	tb["sess_time"] = data.session_time or user_cfg.session_time or ""
	tb["flow"] = data.flow or user_cfg.flow or ""
	tb["smart_mac"] = data.smart_mac or user_cfg.smart_mac or ""

	result = utl.ubus("nac_proxy", "websrv", {act='adduser',data=tb}) or {}

	luci.http.redirect(m.redirect)
    end
     return true
end
return m
